package com.tqjc.system.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.system.common.controller.BaseController;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.ResponseUtil;
import com.tqjc.system.system.entity.SysDepartEntity;
import com.tqjc.system.system.entity.SysGateRecordEntity;
import com.tqjc.system.system.model.bo.SysGateRecordBO;
import com.tqjc.system.system.service.SysGateRecordService;
import com.tqjc.system.system.wrapper.DeptWrapper;
import com.tqjc.system.system.wrapper.GateRecordWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.ws.rs.core.Response;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86195
 * @version 1.0
 * @description 道闸控制层
 * @date 2023/9/22 15:56
 */
@Controller
@RequestMapping("/sys/entrance")
@Slf4j
public class SysEntranceController extends BaseController {

    @Autowired
    private SysGateRecordService gateRecordService;

    /**
     * 道闸管理首页
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model){
        return "sys/entrance/index.html";
    }


    @RequestMapping("/list")
    public String carInfoIndex(Model model){
        return "sys/entrance/carInfo_list.html";
    }


    @RequestMapping("/carInfoList")
    @ResponseBody
    public void carInfoList(SysGateRecordBO sysGateRecordBO, PageInfo<SysGateRecordEntity> page, HttpServletResponse response){
        //结果处理,直接写到客户端
        log.info("查询道闸记录接口开始");
        IPage<SysGateRecordEntity> lstResult = gateRecordService.getListByPage(page.getPageEntity(), sysGateRecordBO);
        ResponseUtil.write(response, new GateRecordWrapper(lstResult).wrap());
        log.info("查询道闸记录接口结束");

    }


    @RequestMapping("/query/id")
    public String queryDetailById(String id,Model model){
        log.info("根据id查询道闸记录接口开始，id：{}",id);

        //结果处理,直接写到客户端
        SysGateRecordEntity sysGateRecord = gateRecordService.queryById(id);
        model.addAttribute("sysGateRecord",sysGateRecord);
        log.info("根据id查询道闸记录接口结束，id：{}",id);

        return "sys/entrance/gateRecordDetail.html";
    }

    @RequestMapping("/getMaterial")
    @ResponseBody
    public AjaxJson getMaterial(String carLicense){
        log.info("根据车牌查询道闸记录接口开始，carLicense：{}",carLicense);

        SysGateRecordEntity sysGateRecord = gateRecordService.getMaterialByCarlicense(carLicense);
        log.info("根据车牌查询道闸记录接口结束，carLicense：{}",carLicense);

        return AjaxJson.ok(sysGateRecord, "获取成功");
    }

    @ResponseBody
    @RequestMapping("/update")
    public AjaxJson update(SysGateRecordBO sysGateRecordBO){
        try {
            log.info("编辑道闸记录接口开始");

            gateRecordService.updateInfo(sysGateRecordBO);
            log.info("编辑道闸记录接口结束");

        } catch (Exception e) {
            return AjaxJson.fail("编辑失败");
        }
        return AjaxJson.ok("编辑成功");

    }


    @ResponseBody
    @RequestMapping("/openEntrance")
    public AjaxJson openEntrance(String gateNameParam,String idKey,String status){

        try {
            log.info("打开道闸接口开始");
            idKey = gateRecordService.openGate(gateNameParam,idKey,status);
            log.info("打开道闸接口结束");
        } catch (Exception e) {
            return AjaxJson.fail("打开道闸失败");
        }

        return AjaxJson.ok(idKey,"打开道闸成功");
    }


    @ResponseBody
    @RequestMapping("/stop")
    public AjaxJson stopOpen(String idKey){

        try {
            log.info("停止打开道闸开始");
            gateRecordService.stopOpen(idKey);
            log.info("停止打开道闸结束");
        } catch (Exception e) {
            return AjaxJson.fail("操作失败");
        }

        return AjaxJson.ok("操作成功");
    }


    @RequestMapping("/add")
    public String add(String id,Model model){
        log.info("根据id查询道闸记录接口开始，id：{}",id);

        //结果处理,直接写到客户端
        SysGateRecordEntity sysGateRecord = gateRecordService.queryById(id);
        model.addAttribute("sysGateRecord",sysGateRecord);
        log.info("根据id查询道闸记录接口结束，id：{}",id);

        return "sys/entrance/addGateRecord.html";
    }



    @RequestMapping("/monitor")
    public AjaxJson monitor(){
        log.info("查看实时监控画面开始");
        AjaxJson json;
        try {
            gateRecordService.monitor();
            log.info("查看实时监控画面结束");
            json = AjaxJson.ok("操作成功");
        } catch (Exception e){
            log.error("查看实时监控画面异常",e);
            json = AjaxJson.fail("操作失败");
        }

        return json;
    }


    /**
     *  接受TMS系统返回结果接口
     * @param resJson  返回结果参数
     * @return
     */
    @PostMapping(value = "/api/getResult")
    @ResponseStatus(value = HttpStatus.OK)
    public Response getResult(@RequestBody String resJson,HttpServletResponse response){
        Response res = null;
        try {

            log.info("接受TMS系统返回车辆信息接口开始,接收参数为：{}",resJson);
            gateRecordService.acceptResult(resJson);
            log.info("接受TMS系统返回车辆信息接口结束,接收参数为：{}",resJson);

            response.setHeader("returnCode","0");
            res = Response.ok().header("returnCode", "0").build();
        } catch (Exception e){
            res = Response.ok().header("returnCode", "1001").build();
            log.error("接受TMS系统返回车辆信息接口出现异常",e);
        }

        return res;
    }









}
